<template>
  <!-- 编辑采购计划 -->
  <a-modal :width="1200" v-model:visible="showEditPlanDialog" :title="`编辑采购计划-${currentData.billNo}`" title-align="start" :mask-closable="false" @cancel="closedDialog" :ok-loading="submitLoading" :on-before-ok="editPurchasePlan">
    <div class="edit-w">
      <PurchaseEditModule type="edit" :currentData="currentData" ref="purchaseEditModule" addModel="purchasePlan" />
    </div>
  </a-modal>
</template>
<script lang="ts" setup>
  import { ref } from 'vue'
  import PurchaseEditModule from './purchaseEditModule.vue'
  import { Message } from '@arco-design/web-vue'
  import { updatePurchasePlan } from '@/api/purchase'

  const emit = defineEmits(['closed', 'success'])
  const props = defineProps({
    isShow: {
      type: Boolean,
      default: false,
    },
    currentData: {
      type: Object,
      default: () => {},
    },
  })

  const showEditPlanDialog = ref(props.isShow)
  const submitLoading = ref(false)
  const purchaseEditModule = ref()

  // 选择产品
  async function editPurchasePlan() {
    let valid = await purchaseEditModule.value.validateData()
    if (!valid) return false
    let saveData = purchaseEditModule.value.getSavaData()
    const res: any = await updatePurchasePlan(saveData)
    if (res) {
      Message.success('保存成功！')
      emit('success', saveData)
      emit('closed')
      return true
    }
    return false
  }
  // 关闭
  function closedDialog() {
    showEditPlanDialog.value = false
    emit('closed')
  }
</script>
<style lang="less" scoped>
  .edit-w {
    height: calc(84vh - 140px);
    overflow-y: hidden;
  }
</style>
